<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.util.*,util.*,dal.*,model.*,bll.*"%>
<%
	PurchaseItemModel obj;
	Object temp = request.getAttribute("purchaseItem");
	if (temp != null) {
		try {
			obj = (PurchaseItemModel) temp;
		} catch (Exception ex) {
			obj = new PurchaseItemModel();
		}
	} else {
		obj = new PurchaseItemModel();
	}
	int idPurchase = obj.getIdPurchase();
	
	
// 	PurchaseModel purchase = CacheUtil.getPurchase(idPurchase);
	
	String purchaseName =CacheUtil.getPurchaseName(idPurchase);
	
	int idProject=obj.getIdProject();
	boolean hasProject=false;
	if(idProject!=Integer.MIN_VALUE)
		hasProject=true;
	
	int idEvent=obj.getIdEvent();
	boolean hasEvent=false;
	if(idEvent!=Integer.MIN_VALUE)
		hasEvent=true;
	
	int idChecklist=obj.getIdChecklist();
	boolean hasChecklist=false;
	if(idChecklist!=Integer.MIN_VALUE)
		hasChecklist=true;
	
	int idTask=obj.getIdTask();
	boolean hasTask=false;
	if(idTask!=Integer.MIN_VALUE)
		hasTask=true;
	
	int idQuotation=obj.getIdQuotation();
	boolean hasQuotation=false;
	if(idQuotation!=Integer.MIN_VALUE)
		hasQuotation=true;
	
	int idQuotationItem=obj.getIdQuotationItem();
	boolean hasQuotationItem=false;
	if(idQuotationItem!=Integer.MIN_VALUE)
		hasQuotationItem=true;
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Edit Purchase Item</title>
<link rel="stylesheet" href="css/common.css" />
<link rel="stylesheet" href="css/inputForm.css" />
<link rel="stylesheet" href="css/listForm.css" />
<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
<link rel="stylesheet" href="css/hyjack.css" type="text/css" />
<link rel="stylesheet" href="css/datepicker.css" type="text/css" />
<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
<link rel="stylesheet" href="css/jquery-ui-timepicker.css" type="text/css" />
<link rel="stylesheet" href="css/jquery-datepicker.css" />
<link rel="stylesheet" href="css/jquery-dialog.css" />

<script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.select.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.core.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.timepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.js"></script>
<script language="javascript" type="text/javascript" src="js/ajaxQuickInsert.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.numeric.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.tools.min.js"></script>
<script language="javascript" type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>


<script language="javascript" type="text/javascript">
$(document).ready(pageLoaded);

function pageLoaded()
{
	turnOnNumbericInput();
	initTooltip();	
	initDateTimePicker();
 	serializeProject();
	
} 

var requiredInputs = [['item_price','item_price_required'],['quantity','quantity_required']];
var intInputs = [];
var doubleInputs = [];
var dateInputs = [];
var timeInputs = [['plan_delivery_time_date','plan_delivery_time_date_invalid'],['real_delivery_time_date','real_delivery_time_date_invalid']];

function checkInputs() {
	var valid = true;

	if (checkRequiredInputs(requiredInputs) == false) {
		valid = false;
	}
	if (checkIntInputs(intInputs) == false) {
		valid = false;
	}
	if (checkDoubleInputs(doubleInputs) == false) {
		valid = false;
	}
	if (checkDateInputs(dateInputs) == false) {
		valid = false;
	}
	if (checkTimeInputsForCorrectFormatOnly(timeInputs) == false) {
		valid = false;
	}
	if (checkOnlyOneSelectInputs('id_product','id_service') == false) {
		valid = false;
	}
	// SPECIFIC VALIDATE CODE HERE		

	return valid;
}
function serializeProject()
{
	getCascadeDropdowList('#id_project','#id_event','event');
	getCascadeDropdowList('#id_project','#id_checklist','checklist');
	getCascadeDropdowList('#id_project','#id_quotation','quotation');
	serializeChecklist();
	serializeQuotation();
 	
}
function serializeQuotation()
{
	getCascadeDropdowList('#id_quotation','#id_quotation_item','quotationItem');
}
function serializeChecklist()
{
	getCascadeDropdowList('#id_checklist','#id_task','checklistItem');
}

</script>
</head>
<body>
	<div class="divContainer">
		<%@ include file="menu.jsp"%>
				<form id="inputForm" action="purchaseItem?mode=update&id_purchase=<%=idPurchase%>" method="post" onSubmit="return checkInputs()" >
					<table class="tblInput">
						<tr>
							<td colspan="7"><b>Purchase:<%=purchaseName%></b></td>
						</tr>
						<tr>
							<td class="inputHeader">EDIT PURCHASE ITEM INFO</td>
						</tr>
						<tr>
					<td style="padding-left: 200px; padding-right: 200px;">
						<table cellspacing="0px">
						<tr class="trField">
							<td class="tdFieldName">Id</td>
							<td class="tdFieldInput">
								<input class="inputInt readonly" type="text" id="id" name="id" readonly="readonly" value="<%=obj.getId()%>" title="Id of this purchase Item. Note: Can not edit." />
								<span class="textGuide"> (Can not edit)</span>
							</td>
							<td class="tdErrMsg"></td>
							
							<td rowspan="11"></td>
							
							<td class="tdFieldName">Discount</td>
							<td class="tdFieldInput">
								<input class="inputNumeric" type="text" id="discount" name="discount" value="<%=obj.getDiscountToString() %>" title="PurchaseItem discount. <br/>Note: numeric only." maxlength="20" />
							</td>
							<td class="tdErrMsg">
							</td>
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Product</td>
							<td class="tdFieldInput">
								<select id="id_product" name="id_product" class="hyjack" onchange="checkOnlyOneSelectInputs('id_product','id_service');" >
									<%=HtmlUtil.<ProductModel>genModelSelect(CacheUtil.getProductList(), null,obj.getIdProduct(), false, true)%>
								</select>
								<input class="btnAdd" type="button" id="addNewProduct" value="" onclick="ajaxAddItem('#id_product', 'product', 'short_name', 'Please enter product name!');" title="Click here to add new product." />
							</td>
							<td class="tdErrMsg">
								<span id="id_product_invalid" class="errMsg" style="display: none;" title="You must select only one Product or Service">(!)</span> 
							</td>
							
							<td class="tdFieldName">Additional Cost</td>
							<td class="tdFieldInput">
								<input class="inputNumeric" type="text" id="additional_cost" name="additional_cost" value="<%=obj.getAdditionalCostToString() %>" title="Additional cost of the purchaseItem. <br/>Note: numeric only." maxlength="20" />
							</td>
							<td class="tdErrMsg">
							</td>
							
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Service</td>
							<td class="tdFieldInput">
								<select id="id_service" name="id_service" class="hyjack" onchange="checkOnlyOneSelectInputs('id_product','id_service');" >
									<%=HtmlUtil.<ServiceModel>genModelSelect(CacheUtil.getServiceList(), null,obj.getIdService(), false, true)%>
								</select>
								<input class="btnAdd" type="button" id="addNewService" value="" onclick="ajaxAddItem('#id_service', 'service', 'short_name', 'Please enter service name!');" title="Click here to add new service." />
							</td>
							<td class="tdErrMsg">
								<span id="id_service_invalid" class="errMsg" style="display: none;" title="You must select only one Product or Service">(!)</span> 
							</td>
							<td class="tdFieldName">Item Cost</td>
							<td class="tdFieldInput">
								<input class="inputNumeric" type="text" readonly="readonly" id="item_cost" name="item_cost" value="<%=obj.getItemCostToString() %>" title="Item cost of the purchaseItem. <br/>Note: Item price * Quantity - Discount + Additional cost." maxlength="20" />
							</td>
							<td class="tdErrMsg">
							</td>
												
												
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Requester</td>
							<td class="tdFieldInput">
								<select id="requester" name="requester" class="hyjack" title="Choose a requester for this purchaseItem.">
								<%=HtmlUtil.<EmployeeModel>genModelSelect(CacheUtil.getEmployeeList(), null, obj.getRequester(), false, true)%>
								</select>
								<input class="btnAdd" type="button" id="addNewRequester" value="" onclick="ajaxAddItem('#requester', 'employee', 'name', 'Please enter new employee name!');" title="Click here to add new employee."/>
							</td>
							<td class="tdErrMsg">
							</td>
							<td class="tdFieldName">Plan Delivery Time</td>
							<td class="tdFieldInput"><input type="text" maxlength="19" class="inputDateTime" id="plan_delivery_time_date" name="plan_delivery_time_date" value="<%=StringUtil.toVisualDateTimeString(obj.getPlanDeliveryTime())%>" title="Click to choose plan delivery time.<br/>Note: the format is <b>dd/mm/yyyy at hh:mm</b>."  onchange="validateDateTimeInput('plan_delivery_time_date', false);" /> 
							</td>
							<td class="tdErrMsg">
								<span id="plan_delivery_time_date_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.<br/>The format is <b>dd/mm/yyyy at hh:mm</b>">(!)</span>
							</td>
							
								
							
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Description</td>
							<td class="tdFieldInput">
								<textarea id="description" name="description" rows="5" class="inputTextarea" title="Description for new purchase item."><%=obj.getDescriptionToString()%></textarea>
							</td>
							<td class="tdErrMsg">
							</td>
							<td class="tdFieldName">Real Delivery Time</td>
							<td class="tdFieldInput"><input type="text" maxlength="19" class="inputDateTime" id="real_delivery_time_date" name="real_delivery_time_date" value="<%=StringUtil.toVisualDateTimeString(obj.getRealDeliveryTime())%>" title="Click to choose real delivery time.<br/>Note: the format is <b>dd/mm/yyyy at hh:mm</b>."  onchange="validateDateTimeInput('real_delivery_time_date', false);" /> 
							</td>
							<td class="tdErrMsg">
								<span id="real_delivery_time_date_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.<br/>The format is <b>dd/mm/yyyy at hh:mm</b>">(!)</span>
							</td>
							
							
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Remark</td>
							<td class="tdFieldInput">
								<textarea id="remark" name="remark" rows="5" class="inputTextarea" title="remark for new purchaseItem."><%=obj.getRemarkToString()%></textarea>
							</td>
							<td class="tdErrMsg">
							</td>
							<td class="tdFieldName">Project</td>
							<td class="tdFieldInput">
							<%if(!hasProject){ %>
								<select id="id_project" name="id_project" class="hyjack" onchange="serializeProject();" >
									<%=HtmlUtil.<ProjectModel>genModelSelect(CacheUtil.getProjectList(), null, Integer.MIN_VALUE, false, true)%>
								</select>
								<%}else{ %>
								<input  type="hidden" id="id_project" name="id_project" value="<%=idProject %>" />
								<input class="inputText" readonly="readonly" title="Project of this purchase item. Note: Read only" type="text" value="<%=CacheUtil.getProjectName(idProject) %>"  maxlength="255" />
								<%}%>
							</td>
							<td class="tdErrMsg">
							</td>	
							
							
							
						</tr>
						<tr>
							<td class="tdFieldName">Status</td>
							<td class="tdFieldInput"><select id="status" name="status" class="hyjack" title="Choose one status for the purchase item.">
								<%=HtmlUtil.genEnumSelect(EnumName.PurchaseItemStatus, null,obj.getStatus()) %>
								</select>
							</td>
							<td class="tdErrMsg"></td>
							<td class="tdFieldName">Event</td>
							<td class="tdFieldInput">
							<%if(!hasEvent){ %>
								<select id="id_event" name="id_event" class="hyjack" >
									<option value="">---------------------</option>
								</select>
							<%} else{ %>
								<input  type="hidden" id="id_event" name="id_event" value="<%=idEvent %>" />
								<input class="inputText" readonly="readonly" title="Event of this purchase item. Note: Read only" type="text" value="<%=CacheUtil.getEventName(idEvent) %>"  maxlength="255" />
							<%} %>
							</td>
							<td class="tdErrMsg">
							</td>
								
																				
						</tr>
						<tr>
							<td class="tdFieldName">Unit</td>
							<td class="tdFieldInput">
								<select id="id_unit" name="id_unit" class="hyjack" >
									<%=HtmlUtil.<UnitModel>genModelSelect(CacheUtil.getUnitList(), null,obj.getIdUnit())%>
								</select>
							</td>
							<td class="tdErrMsg">
							</td>	
							<td class="tdFieldName">Checklist</td>
							<td class="tdFieldInput">
							<%if(!hasChecklist){ %>
								<select id="id_checklist" name="id_checklist" class="hyjack" onchange="serializeChecklist();" >
									<option value="">---------------------</option>
								</select>
							<%} else{ %>
								<input  type="hidden" id="id_checklist" name="id_checklist" value="<%=idChecklist %>" />
								<input class="inputText" readonly="readonly" title="Checklist of this purchase item. Note: Read only" type="text" value="<%=CacheUtil.getChecklistName(idChecklist) %>"  maxlength="255" />
							<%} %>
							</td>
							<td class="tdErrMsg">
							</td>	
							
														
						</tr>
						<tr>
							<td class="tdFieldName">Item Price</td>
							<td class="tdFieldInput">
								<input class="inputNumeric" type="text" id="item_price" name="item_price" value="<%=obj.getItemPriceToString() %>" title="Item price of the purchaseItem. <br/>Note: Numeric only.<br/>Note: Required." maxlength="20" onblur="validateRequiredInput('item_price');" />
							</td>
							<td class="tdErrMsg">
								<span id="item_price_required" class="errMsg" style="display: none;" title="Item price is required">(!)</span> 
							</td>
							<td class="tdFieldName">Task</td>
							<td class="tdFieldInput">
							<%if(!hasTask){ %>
								<select id="id_task" name="id_task" class="hyjack" >
									<option value="">---------------------</option>
								</select>
							<%} else{ %>
								<input  type="hidden" id="id_task" name="id_task" value="<%=idTask %>" />
								<input class="inputText" readonly="readonly" title="Task of this purchase item. Note: Read only" type="text" value="<%=CacheUtil.getChecklistItemName(idTask) %>"  maxlength="255" />
							<%} %>
							</td>
							<td class="tdErrMsg">
							</td>	
							
							
							
						</tr>
						<tr>
							<td class="tdFieldName">Quantity</td>
							<td class="tdFieldInput">
								<input class="inputNumeric" type="text" id="quantity" name="quantity" value="<%=obj.getQuantityToString() %>" title="Quantity of the purchaseItem. <br/>Note: Numeric only.<br/>Note: Required." maxlength="20" onblur="validateRequiredInput('quantity');" />
							</td>
							<td class="tdErrMsg">
								<span id="quantity_required" class="errMsg" style="display: none;" title="Quantity is required">(!)</span> 
							</td>
							<td class="tdFieldName">Quotation</td>
							<td class="tdFieldInput">
							<%if(!hasQuotation){ %>
								<select id="id_quotation" name="id_quotation" class="hyjack" onchange="serializeQuotation();" >
									<option value="">---------------------</option>
								</select>
							<%} else{ %>
								<input  type="hidden" id="id_quotation" name="id_quotation" value="<%=idQuotation %>" />
								<input class="inputText" readonly="readonly" title="Quotation of this purchase item. Note: Read only" type="text" value="<%=CacheUtil.getQuotationName(idQuotation) %>"  maxlength="255" />
							<%} %>
							</td>
							<td class="tdErrMsg">
							</td>	
										
							
						</tr>
						<tr>
							<td colspan="3"></td>
							<td class="tdFieldName">Quotation Item</td>
							<td class="tdFieldInput">
							<%if(!hasQuotationItem){ %>
								<select id="id_quotation_item" name="id_quotation_item" class="hyjack" >
									<option value="">---------------------</option>
								</select>
							<%} else{ %>
								<input  type="hidden" id="id_quotation_item" name="id_quotation_item" value="<%=idQuotationItem %>" />
								<input class="inputText" readonly="readonly" title="Quotation item of this purchase item. Note: Read only" type="text" value="<%=CacheUtil.getQuotationItemName(idQuotationItem) %>"  maxlength="255" />
							<%} %>
							</td>
							<td class="tdErrMsg">
							</td>	
						</tr>
						
						<tr class="trInputBottom">
							<td colspan="7" class="tdSubmit" style="text-align: center;"><input class="btnSubmit" type="submit" value="Submit" title="Click to save above information."/></td>

						</tr>
					</table>
				</td>
			</tr>
		</table>
	</form>
	<%
	
		String urlPurchaseItemList = "purchaseItemList.jsp?embedded=true&idPurchase=" + obj.getIdPurchase();
	%>
		<jsp:include page='<%=urlPurchaseItemList%>'></jsp:include>
	</div>
</body>
</html>